import { createRouter, createWebHistory } from 'vue-router'
import Index from '@/views/index.vue'
import Home from '@/views/home.vue'
import Cla from '@/views/cla.vue'
import Cart from '@/views/cart.vue'
import My from '@/views/my.vue'
import Search from '@/views/search/search.vue'
import Login from '@/views/login/login.vue'
import Register from '@/views/register/register.vue'
import Detail from '@/views/detail/detail.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/index/home' },
    {
      path: '/index', name: 'index', component: Index, children: [
        { path: 'home', name: 'home', component: Home, },
        { path: 'cla', name: 'cla', component: Cla, },
        { path: 'cart', name: 'cart', component: Cart, },
        { path: 'my', name: ' my', component: My, },
      ]
    },
    { path: '/search', name: ' search', component: Search, },
    { path: '/login', name: ' login', component: Login, },
    { path: '/register', name: ' register', component: Register, },
    { path: '/detail/:goods_id', name: ' detail', component: Detail, },


  ],
})
router.beforeEach((to,  from,next) => {
  if (to.fullPath == '/index/cart' || to.fullPath == '/index/my') {
    let token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  }else{
    next()
  }


})
export default router
